<?php

/**
 * trims a string to a certain length, returns escaped
 *
 * @author    Mon Zafra <monzee at gmail>
 * @copyright (c)2009 Mon Zafra
 * @license   http://monzee.wordpress.com/license New BSD
 * @version   $Id: Truncate.php 38 2009-02-18 17:12:39Z monzee $
 */
class Crud_Truncate
{
    protected $_view;

    public function setView($view)
    {
        $this->_view = $view;
    }

    public function truncate($str, $len = 50)
    {
        if (null === $str || '' == $str) {
            return '<em>NULL</em>';
        }
        
        if (strlen($str) > $len) {
            $str = rtrim(substr($str, 0, $len));
            return $this->_view->escape($str) . '&hellip;';
        }

        return $this->_view->escape($str);
    }
    
}
